#!/usr/bin/env python

# http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

import Tkinter as tk
import ttk

from grideditor import *
from classtab import *
from courselisttab import *

import os
import glob
from Tkinter import PhotoImage




def GPYGuiInit():
	root = tk.Tk()
	GPYGuiCreateTheme()
	# install('/Users/chris/Downloads/')
	app = GPYApplication()
	app.mainloop()

def GPYGuiCreateTheme():
	s = ttk.Style()
	
	if s.theme_use() == 'aqua':
		# fix tab text positioning issue
		# http://www.tcl.tk/man/tcl/TkCmd/ttk_notebook.htm
		# http://stackoverflow.com/questions/23038356/change-color-of-tab-header-in-ttk-notebook
		s.theme_create('aquafix', parent = 'aqua', settings = {
			'TNotebook': {'configure': {'tabmargins': [2, 5, 2, 0]}},
			'TNotebook.Tab': {
				'configure': {'padding': [12, 7, 12, 0]}#, 'background': '#d2ffd2'}
				# 'layout': {'children': [('Notebook.padding', {'children': [('Notebook.label', {'expand': '1', 'sticky': 'nswe'})], 'expand': '1', 'sticky': 'nswe'})], 'sticky': 'nswe'}
				# 'map': {
				# 	'background': [('selected', '#dd0202')],
				# 	'expand': [('selected', [1, 1, 1, 0])]
				# }
			}
		})
		s.theme_use('aquafix')
		# s.layout('TNotebook.Tab',{})


class GPYApplication(ttk.Frame):
	def __init__(self, master = None):
		ttk.Frame.__init__(self, master)
		self.master.title('GradePy')
		self.grid(sticky = 'nsew')
		
		#self.grid_propagate(0)
		#(self['width'], self['height']) = (200, 200)
		
		self.CreateWidgets()
		self.CreateMenuBar()
		self.CreateWindows()
	
	def CreateWidgets(self):
		top = self.winfo_toplevel()
		
		top.rowconfigure(0, weight = 1)
		top.columnconfigure(0, weight = 1)
		self.rowconfigure(0, weight = 1)
		self.columnconfigure(0, weight = 1)
		
		self.tabBar = ttk.Notebook(self)
		self.tabBar.bind('<<NotebookTabChanged>>', lambda x:0)
		self.tabBar.grid(row = 0, column = 0, sticky = 'nsew')
		
		self.courselisttab = CourseListTab(self.tabBar)
		self.courselisttab.grid(row = 0, column = 0, sticky = 'nsew')
		self.tabBar.add(self.courselisttab, text = "Classes")
		
		self.gridEditor = GridEditor(self.tabBar, 3, 4)
		self.gridEditor.grid(row = 0, column = 0, sticky = 'nsew')
		self.tabBar.add(self.gridEditor, text = "Class 1")
		
		self.gridEditor2 = GridEditor(self.tabBar, 3, 4)
		self.gridEditor2.grid(row = 0, column = 0, sticky = 'nsew')
		self.tabBar.add(self.gridEditor2, text = "Class 2")
		
		self.generalBottomBar = ttk.Frame(self)
		self.generalBottomBar.grid(row = 1, column = 0, sticky = 'w')
		
		self.studentButton = ttk.Button(self.generalBottomBar, text = 'Add Student', default = tk.NORMAL, command = self.AddStudent)
		self.studentButton.grid(row = 0, column = 0)
		
		self.button2 = ttk.Button(self.generalBottomBar, text = 'Button 2', default = tk.NORMAL)
		self.button2.grid(row = 0, column = 1)
		
		self.button3 = ttk.Button(self.generalBottomBar, text = 'Button 3', default = tk.NORMAL)
		self.button3.grid(row = 0, column = 2)
		
		self.specificBottomBar = ttk.Frame(self)
		self.specificBottomBar.grid(row = 2, column = 0, sticky = 'w')
		
		self.button4 = ttk.Button(self.specificBottomBar, text = 'Button 4', default = tk.NORMAL)
		self.button4.grid(row = 0, column = 0)
		
		self.button5 = ttk.Button(self.specificBottomBar, text = 'Button 5', default = tk.NORMAL)
		self.button5.grid(row = 0, column = 1)
		
		self.quitBar = ttk.Frame(self)
		self.quitBar.grid(row = 3, column = 0, sticky = 'n')
		
		self.quitButton = ttk.Button(self.quitBar, text = 'Quit', default = tk.ACTIVE, command = self.quit)
		self.quitButton.grid_propagate(0)
		#self.quitButton['width'] = self.quitButton.reqwidth()
		#self.quitButton['height'] = self.quitButton.reqheight()
		self.quitButton.grid(row = 0, column = 0)
		
		top.bind('<Return>', (lambda e, b=self.quitButton: b.invoke()))
	
	def CreateWindows(self):
		self.studentEditorWindow = tk.Toplevel()
		self.studentEditor = GPYStudentEditor(self.studentEditorWindow)
		
		self.studentEditorWindow.withdraw()
	
	def CreateMenuBar(self):
		self.menuBar = tk.Menu(self.winfo_toplevel())
		
		self.filemenu = tk.Menu(self.menuBar)
		self.filemenu.add_command(label='New Gradebook...', command=self.AboutHandler)
		self.filemenu.add_command(label='Open Gradebook...', command=self.AboutHandler)
		self.menuBar.add_cascade(label='File', menu=self.filemenu)
		
		self.mymenu = tk.Menu(self.menuBar)
		self.mymenu.add_command(label='About', command=self.AboutHandler)
		self.mymenu.add_command(label='Help!!!!', command=self.HelpHandler)
		self.menuBar.add_cascade(label='MyMenu', menu=self.mymenu)
		
		top = self.winfo_toplevel()
		top['menu'] = self.menuBar
	
	def AddStudent(self):
		self.studentEditorWindow.deiconify()
	
	def AboutHandler(self):
		print "About!!!!"
	
	def HelpHandler(self):
		print "Help!!!!"

class GPYStudentEditor(ttk.Frame):
	def __init__(self, master = None):
		ttk.Frame.__init__(self, master)
		self.master.title('Create New Student')
		self.grid(sticky = 'nsew')
		
		self.CreateWidgets()
	
	def CreateWidgets(self):
		top = self.winfo_toplevel()
		
		frame1 = ttk.Frame(self)
		frame1.grid(row = 0, column = 0, sticky = 'nsew')
		
		self.firstLabel = ttk.Label(frame1, text="First Name")
		self.firstLabel.grid(row = 0, column = 0, sticky = 'w')
		self.firstEntry = ttk.Entry(frame1)
		self.firstEntry.grid(row = 0, column = 1, sticky = 'ew')
		
		self.lastLabel = ttk.Label(frame1, text="Last Name")
		self.lastLabel.grid(row = 1, column = 0, sticky = 'w')
		self.lastEntry = ttk.Entry(frame1)
		self.lastEntry.grid(row = 1, column = 1, sticky = 'ew')
		
		frame2 = ttk.Frame(self)
		frame2.grid(row = 1, column = 0, sticky = 'e')
		
		self.cancelButton = ttk.Button(frame2, text = 'Cancel', default = tk.NORMAL, command = self.winfo_toplevel().destroy)
		self.cancelButton.grid(row = 0, column = 0, sticky = 'e')
		
		self.addButton = ttk.Button(frame2, text = 'Add', default = tk.ACTIVE, command = self.AddStudent)
		self.addButton.grid(row = 0, column = 1, sticky = 'e')
	
	def AddStudent(self):
		first = self.firstEntry.get()
		last = self.lastEntry.get()
		print("Adding student " + first + " " + last)
		

	

#GPYGuiInit()

